//
//  SecondViewController.m
//  Album
//
//  Created by Mars_ZXB on 16/7/20.
//  Copyright © 2016年 BAT3. All rights reserved.
//

#import "SecondViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface SecondViewController ()<UIScrollViewDelegate>
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)UIPageControl *page;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //self.navigationController.navigationBar.translucent = NO;
    
    
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    self.scroll.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.scroll];
    self.scroll.pagingEnabled = YES;
    self.scroll.bounces = NO;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.contentSize = CGSizeMake(WIDTH * 9, HEIGHT - 64);
    
    
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(100, HEIGHT - 50 - 64, 200, 50)];
    [self.view addSubview:self.page];
    self.page.backgroundColor = [UIColor clearColor];
    self.page.numberOfPages = 7;
    self.page.pageIndicatorTintColor = [UIColor redColor];
    self.page.currentPageIndicatorTintColor = [UIColor greenColor];
    [self.page addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    
    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,HEIGHT - 64)];
    imagev.image = [UIImage imageNamed:@"h6.jpeg"];
    [self.scroll addSubview:imagev];
    for (NSInteger i = 0; i < 7; i++ ) {
        UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT - 64)];
        imagev.image = [UIImage imageNamed:[NSString stringWithFormat:@"h%ld.jpeg", i ]];
        [self. scroll addSubview:imagev];
    }
    UIImageView *imagev1 = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH * 8, 0, self.view.frame.size.width, HEIGHT - 64)];
    imagev1.image = [UIImage imageNamed:@"h0.jpeg"];
    [self.scroll addSubview:imagev1];
    
    self.scroll.contentOffset = CGPointMake(WIDTH, 0);
    self.scroll.delegate = self;
    
    switch (self.number) {
        case 1000:
            self.scroll.contentOffset = CGPointMake(WIDTH, 0);
            self.title = @"第1页";
            self.page.currentPage = 0;
            break;
        case 1001:
            self.scroll.contentOffset = CGPointMake(WIDTH * 2, 0);
            self.title = @"第2页";
            self.page.currentPage = 1;
            break;
        case 1002:
            self.scroll.contentOffset = CGPointMake(WIDTH * 3, 0);
            self.title = @"第3页";
            self.page.currentPage = 2;
            break;
        case 1003:
            self.scroll.contentOffset = CGPointMake(WIDTH * 4, 0);
            self.title = @"第4页";
            self.page.currentPage = 3;
            break;
        case 1004:
            self.scroll.contentOffset = CGPointMake(WIDTH * 5, 0);
            self.title = @"第5页";
            self.page.currentPage = 4;
            break;
        case 1005:
            self.scroll.contentOffset =CGPointMake(WIDTH * 6, 0);
            self.title = @"第6页";
            self.page.currentPage = 5;
            break;
        case 1006:
            self.scroll.contentOffset = CGPointMake(WIDTH * 7, 0);
            self.title = @"第7页";
            self.page.currentPage = 7;
            break;
            
        default:
            break;
    }
    
    
    
    
}

-(void)click:(UIPageControl *)page{
    [self.scroll setContentOffset:CGPointMake(WIDTH * (page.currentPage + 1), 0) animated:YES];
    self.title = (NSString *)[NSString stringWithFormat:@"第%ld页",self.page.currentPage + 1];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.scroll.contentOffset.x == 0) {
        self.scroll.contentOffset = CGPointMake(WIDTH * 7, 0);
    }else if (self.scroll.contentOffset.x == WIDTH * 8){
        self.scroll.contentOffset = CGPointMake(WIDTH, 0);
    }
    self.page.currentPage = scrollView.contentOffset.x / WIDTH - 1;
    self.title = (NSString *)[NSString stringWithFormat:@"第%ld页",self.page.currentPage + 1];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
